package com.example.test.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.test.R;
import com.example.test.entity.Song;
import com.example.test.util.MusicUtils;

import java.util.List;

public class MusicAdapter extends BaseAdapter {

    private List<Song> list;
    private Context context;

    public MusicAdapter(List<Song> list,Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh;
        if (convertView == null){
            vh = new VH();

            convertView = LayoutInflater.from(context).inflate(R.layout.music_layout, null);

            vh.title = convertView.findViewById(R.id.mname);
            vh.artist = convertView.findViewById(R.id.pname);
            vh.durartion = convertView.findViewById(R.id.shichang);

            convertView.setTag(vh);
        }else {
            vh = (VH) convertView.getTag();
        }

        vh.title.setText(list.get(position).getTitle());
        vh.artist.setText(list.get(position).getArtist());
        //调用方法格式时间
        String duration = list.get(position).getDuration();
        vh.durartion.setText(MusicUtils.formatTime(Integer.parseInt(duration)));

        return convertView;
    }
    class VH{
        TextView title;
        TextView artist;
        TextView durartion;
    }
}
